package com.epoint.mobileoa.bizlogic;

import java.util.HashMap;
import java.util.Map;

import android.os.Message;
import android.view.View;

import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.BackServiceUtil;
import com.epoint.mobileoa.tools.LogHelp;

/**
 * @author liyc
 * @time 2012-12-14 上午10:45:37
 * @annotation  4.0框架新任务，新任务类型继承之
 */
public abstract class NewTask {

	/**
	 * 传递过来的键值对参数
	 */
	private Map<String, Object> param;
	
	/**
	 * 任务类别
	 */
	private int taskId;

	/**
	 * 上下文
	 */
	public SuperView sv;

	/**
	 * 日志记录工具类
	 */
	public LogHelp mLog;

	/**
	 * 两个滚动条一起显示
	 */
	public static final int ProgressBoth = 1;

	/**
	 * 至显示顶部滚动条
	 */
	public static final int ProgressTop = 2;

	/**
	 * 显示弹出对话框滚动条
	 */
	public static final int ProgressMiddle = 3;

	/**
	 * 不显示等待对话框
	 */
	public static final int ProgressNo = 4;
	
	public FrameParam fp;

	public NewTask(SuperView sv,int taskID, Map<String, Object> param, String progressMsg,
			int progressDialogType) {
		this.taskId = taskID;
		sv.isTaskExecuteFlag = true;
		this.param = param;
		this.sv = sv;
		fp = sv.fp;
		boolean isServiceRuning = BackServiceUtil.isServiceRunning(sv,
				MainService.Name);

//		LogHelp.LogI("服务开启状态：" + isServiceRuning);
		if (!isServiceRuning) {
//			LogHelp.LogI("主服务重新启动");
			// 启动服务
			BackServiceUtil.startMainService(sv);
		}

		if (progressDialogType == ProgressBoth) {
			sv.top_pb.setVisibility(View.VISIBLE);
			sv.showDialog_wait(progressMsg);
		} else if (progressDialogType == ProgressTop) {
			sv.top_pb.setVisibility(View.VISIBLE);
		} else if (progressDialogType == ProgressMiddle) {
			sv.showDialog_wait(progressMsg);
		} else if (progressDialogType == ProgressNo) {
			
		}
		
		MainService.addNewTask(this);
	}

	public abstract void exectue(Message mess) throws Exception;

	public void setMessageToRefreshUI(Message mess, Object data) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("context", sv);
		map.put("data", data);
		map.put("taskId", taskId);
		mess.obj = map;
	}

	/**
	 * @return the param
	 */
	public Map<String, Object> getParam() {
		return param;
	}

	/**
	 * @param param
	 *            the param to set
	 */
	public void setParam(Map<String, Object> param) {
		this.param = param;
	}

	/**
	 * @return the sv
	 */
	public SuperView getSv() {
		return sv;
	}

	/**
	 * @param sv
	 *            the sv to set
	 */
	public void setSv(SuperView sv) {
		this.sv = sv;
	}

	public String generateURL(String... param) {
		return sv.fp.generateUrl(param);
	}
	
}
